package com.apex.aolonfitdemo.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.apex.aolonfit.AolonFitManager;
import com.apex.aolonfitdemo.R;
import com.apex.aolonfitdemo.databinding.ActivityLocalWatchFaceBinding;
import com.apex.aolonfitdemo.dialog.MonthDialog;
import com.apex.aolonfitdemo.dialog.WaitingDialog;
import com.apex.ax_bluetooth.core.EABleManager;
import com.apex.ax_bluetooth.enumeration.EABleConnectState;
import com.cyber.bridge.module.bean.RaiseLightScreenEntity;
import com.cyber.bridge.module.listener.OnCyberWatchFaceListener;

public class LocalWatchFaceActivity extends AppCompatActivity {
    ActivityLocalWatchFaceBinding activityLocalWatchFaceBinding;
    private WaitingDialog waitingDialog;
    MonthDialog monthDialog;
    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x40) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                activityLocalWatchFaceBinding.restScreen.setText((CharSequence) msg.obj);


            } else if (msg.what == 0x42) {

                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }

            } else if (msg.what == 0x43) {

                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(LocalWatchFaceActivity.this, getString(R.string.modification_failed), Toast.LENGTH_SHORT).show();
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityLocalWatchFaceBinding = ActivityLocalWatchFaceBinding.inflate(getLayoutInflater());
        setContentView(activityLocalWatchFaceBinding.getRoot());
        activityLocalWatchFaceBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(activityLocalWatchFaceBinding.tool);
        getSupportActionBar().setTitle(null);
        activityLocalWatchFaceBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        AolonFitManager.getInstance().setOnWatchFaceListener(new OnCyberWatchFaceListener() {
            @Override
            public void queryDisplayWatchFace(@NonNull String s) {
                if (mHandler != null) {
                    Message message = Message.obtain();
                    message.what = 0x40;
                    message.obj = s;
                    mHandler.sendMessage(message);
                }

            }

            @Override
            public void setDisplayWatchFace(boolean b) {
                if (mHandler != null) {
                    mHandler.sendEmptyMessage(b ? 0x42 : 0x43);
                }

            }
        });
        if (waitingDialog == null) {
            waitingDialog = new WaitingDialog(LocalWatchFaceActivity.this);
        }
        waitingDialog.show();
        AolonFitManager.getInstance().queryDisplayWatchFace();

        activityLocalWatchFaceBinding.restScreen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (monthDialog == null) {
                    monthDialog = new MonthDialog(LocalWatchFaceActivity.this, getString(R.string.watch_face));
                    monthDialog.setSelectListener(new MonthDialog.SelectListener() {
                        @Override
                        public void selectData(int sex) {
                            activityLocalWatchFaceBinding.restScreen.setText(sex + "");
                            waitingDialog.show();
                            AolonFitManager.getInstance().setDisplayWatchFace(sex + "");

                        }
                    });
                }
                if (!monthDialog.isShowing()) {
                    monthDialog.show();
                }

            }
        });
    }

    @Override
    protected void onDestroy() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (monthDialog != null) {
            monthDialog.dismiss();
            monthDialog.destroyDialog();
            monthDialog = null;
        }
        super.onDestroy();
    }
}
